package org.unicat.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.unicat.commonutils.R;
import org.unicat.serviceedu.entity.EduTeacher;
import org.unicat.serviceedu.entity.vo.TeacherQuery;
import org.unicat.serviceedu.service.EduTeacherService;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author unicat
 * @since 2022-02-12
 */
@RestController
@RequestMapping("/eduservice/teachers")
@Api(tags = "讲师管理")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @GetMapping("/")
    @ApiOperation(value = "查找所有讲师")
    public R findAll() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查找讲师")
    public R findTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true)
                             @PathVariable("id") String id) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);

        if (id != null) {
            EduTeacher teacher = teacherService.getById(id);
            if (teacher != null) {
                return R.ok()
                        .data("teacher", teacher);
            } else {
                return R.error()
                        .message("用户不存在");
            }
        } else {
            return R.error()
                    .message("ID不能为空");
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "按ID逻辑删除讲师")
    public R deleteTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                           @PathVariable("id") String id) {
        if (teacherService.removeById(id)) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @GetMapping("/page/{current}/{limit}")
    @ApiOperation(value = "分页查询讲师")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true)
                             @PathVariable("current") Long current,
                             @ApiParam(name = "limit", value = "限制", required = false)
                             @PathVariable("limit") Long limit) {

        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        teacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok()
                .data("total", total)
                .data("rows", records);
    }


    @PostMapping("/pageCondition/{current}/{limit}")
    @ApiOperation(value = "多条件组合分页查询讲师")
    public R pageListTeacherCondition(@ApiParam(name = "current", value = "当前页", required = true)
                                      @PathVariable("current") Long current,
                                      @ApiParam(name = "limit", value = "限制", required = false)
                                      @PathVariable("limit") Long limit,
                                      @ApiParam(name = "teacherQuery", value = "教师视图vo对象", required = true)
                                      @RequestBody(required = false) TeacherQuery teacherQuery) {

        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        // 判断是否为空
        if (!StringUtils.isEmpty(name)) {
            teacherWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            teacherWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            teacherWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            teacherWrapper.le("gmt_create", end);
        }

        teacherService.page(pageTeacher, teacherWrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok()
                .data("total", total)
                .data("rows", records);
    }


    @PostMapping("/")
    @ApiOperation(value = "添加教师信息")
    public R addTeacher(@ApiParam(name = "teacher", value = "教师信息", required = true)
                        @RequestBody EduTeacher teacher) {

        boolean flag = teacherService.save(teacher);
        return flag ? R.ok() : R.error();
    }


    @PutMapping("/")
    @ApiOperation(value = "修改教师信息")
    public R updateTeacher(@ApiParam(name = "teacher", value = "教师信息", required = true)
                           @RequestBody EduTeacher teacher) {

        if (teacherService.updateById(teacher)) {
            return R.ok()
                    .message("更新成功");
        } else {
            return R.ok()
                    .message("更新失败");
        }
    }

//    @GetMapping("/exceptionTest")
//    public R exceptionTest() {
//        try {
//            int i = 10 / 0;
//        } catch (Exception e) {
//            throw new GuliException(20001, "执行了自定义错误...");
//        }
//        return R.ok();
//    }
}

